<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/11/20
 * Time: 11:10
 * BannerController.php
 */

namespace App\Http\Controllers\Api\Shop;


use App\Http\Controllers\Api\BaseAdminController;
use App\Http\Requests\Supplier\UpdateSupplierRequest;
use App\Http\Requests\Supplier\AddSupplierRequest;
use App\Http\Requests\Supplier\DeleteSupplierRequest;
use App\Http\Requests\Supplier\GetSupplierPageRequest;
use App\Http\Transformers\Supplier\GetSupplierPageTransformer;
use App\Repositories\SupplierRepository;

/**
 * Class SupplierController
 * @package App\Http\Controllers\Api\Shop
 */
class SupplierController extends BaseAdminController
{
    /**
     * @var SupplierRepository 注入的数据层
     */
    protected $supplierRepository;

    /**
     * BannerController constructor.
     * @param SupplierRepository $bannerRepository
     */
    public function __construct(SupplierRepository $supplierRepository)
    {
        $this->supplierRepository = $supplierRepository;
    }

    /**
     * 获取rule分页列表
     * @param GetSupplierPageRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getSupplierPage(GetSupplierPageRequest $request){
        $limit = $request->get('limit');
        $data = $request->except(['limit', 'page']);
        $page = $this->supplierRepository->getSupplierPageRepository($data, $limit);
        return $this->response->paginator($page, new GetSupplierPageTransformer());
    }

    /**
     *软删除规则
     * @param DeleteSupplierRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deleteSupplierById(DeleteSupplierRequest $request){
        $id = $request->get('id');
        $result = $this->supplierRepository->deleteSupplierByIdRepository($id);
        if($result)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('删除失败！');
        }
    }

    /**
     * 更新规则
     * @param UpdateSupplierRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateSupplier(UpdateSupplierRequest $request){
        if( !$data = $request->all() ){
            $this->response->errorBadRequest('缺少更新参数');
        }
        $result=$this->supplierRepository->updateSupplierRepository($data);
        if($result)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('更新失败！');
        }

    }

    /**
     * 更新规则状态
     * @param UpdateSupplierRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateSupplierStatus(UpdateSupplierRequest $request){
        if( !$data = $request->all() ){
            $this->response->errorBadRequest('缺少更新参数');
        }
        $result=$this->supplierRepository->updateSupplierStatusRepository($data);
        if($result)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('更新失败！');
        }
    }

    /**
     * 添加拍卖规则
     * @param AddSupplierRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function addSupplier(AddSupplierRequest $request){
        $data = $request->all();
        $add = $this->supplierRepository->addSupplierRepository($data);
        if($add)
        {
            return $this->response->accepted('', 'success');
        }else
        {
            $this->response->errorInternal('更新失败！');
        }

    }
    /**
     * 获取列数据
     * @param GetSupplierPageRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getSupplierList(GetSupplierPageRequest $request){
        $page = $this->supplierRepository->getSupplierListRepository();
        return $this->response->paginator($page, new GetSupplierPageTransformer());
    }
}
